﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace UrlChecker.Setting
{
    public partial class Keywords : QT.Windows.Shell.GlassForm
    {
        public Keywords()
        {
            InitializeComponent();
            loadData();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            this.statusStrip1.BackColor = AeroColor;
        }

        private void dgvKeyword_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            this.tsslMsg.Text = string.Format("共{0}个关键字", this.dgvKeyword.RowCount);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.tbKeywords.Text))
            {
                MessageBox.Show("请输入关键字");
                return;
            }

            int rval = QT.SEO.Keyword.Add(this.tbKeywords.Text);
            switch (rval)
            {
                case -2:
                    MessageBox.Show("关键字已存在");
                    break;
                case -1:
                case 0:
                    MessageBox.Show("操作失败");
                    break;
                case 1:
                    MessageBox.Show("操作成功");
                    loadData();
                    break;
                default:
                    MessageBox.Show("内部异常，错误代码："+rval);
                    break;
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.dgvKeyword.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择要删除的记录");
                return;
            }

            List<string> guids=new List<string>();
            foreach(DataGridViewRow row in this.dgvKeyword.SelectedRows) guids.Add(row.Cells[1].Value.ToString());

            if (QT.SEO.Keyword.Delete(guids) < 1) MessageBox.Show("操作失败");
            else loadData();
        }

        private void loadData()
        {
            DataTable dt = QT.SEO.Keyword.GetKeywords();
            this.dgvKeyword.AutoGenerateColumns = false;
            this.dgvKeyword.Columns[0].DataPropertyName = "keyword";
            this.dgvKeyword.Columns[1].DataPropertyName = "guid";
            this.dgvKeyword.DataSource = dt;
            this.dgvKeyword.AutoResizeColumns();
        }
    }
}
